package com.google.googlemarket.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.google.googlemarket.bean.HomeBean;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/9/19.
 */

public abstract class MyBaseAdapter<T> extends BaseAdapter {

    public ArrayList<T> datas;

    public MyBaseAdapter(ArrayList<T> datas ){
        this.datas = datas;
    }

    public ArrayList<T> getDatas(){
        if (datas==null){
            datas = new ArrayList<T>();
        }
        return datas;
    }

    @Override
    public int getCount() {
        return datas==null?0:datas.size();
    }

    @Override
    public Object getItem(int i) {
        return datas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public final View getView(int position, View convertView, ViewGroup parent) {
        Object holder;
        if (convertView==null){
            convertView = View.inflate(parent.getContext(), getItemLayoutId(position), null);
            holder = createViewHolder(convertView,position);
            convertView.setTag(holder);
        }else{
            holder = convertView.getTag();
        }
        T data = datas.get(position);
        showData(position,holder,data);
        return convertView;
    }

    protected abstract int getItemLayoutId(int position);

    protected abstract Object createViewHolder(View convertView, int position);

    protected abstract void showData(int position, Object holder, T data);
}
